package ejb.contentManagement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ejb.dto.AirportDTO;
import entities.Airport;
import entities.Location;

@Stateless
public class AirportBean implements AirportMgr {
	
	@PersistenceContext
    private EntityManager em;
	
	/**
	 * Retrieve entity Airport from the associated Id
	 * @param id
	 * @return entity Airport associated to the <code>id</code>
	 */
	public Airport getAirport(int id) {
		return em.find(Airport.class, id);
	}
	
	/**
	 * Retrieve all Airport entities
	 * @return all Airport entities
	 */
	public List<Airport> getAllAirports() {
    	return em.createNamedQuery("Airport.findAll", Airport.class)
                .getResultList();
    }
	
	/**
	 * Retrieve entity Airport from the associated Id and convert it to corresponding AirportDTO
	 */
	@Override
	public AirportDTO getAirportDTO(int id) {
		return  convertToDTO(getAirport(id));
    }
	
	/**
	 * Retrieve all Airport entities and convert them into corresponding list of AirportDTO
	 */
	@Override
	public List<AirportDTO> getAllAirportsDTO() {
		List<Airport> list = getAllAirports();
		List<AirportDTO> newList = new ArrayList<AirportDTO>();
		for(Airport obj : list) {
			newList.add(convertToDTO(obj));
		}
		return newList;
	}
	
	/**
	 * Convert entity Airport (parameter <code>a</code>) to corresponding AirportDTO
	 * @param a
	 * @return converted AirportDTO
	 */
	static AirportDTO convertToDTO(Airport a) {
		if(a == null){
			return null;
		}
		AirportDTO dto = new AirportDTO();		
		dto.setId(a.getId());
		dto.setName(a.getName());
		dto.setAddress(a.getAddress());
		dto.setLocation(LocationBean.convertToDTO(a.getLocation()));
		return dto;
	}
	
	/**
	 * Retrieve entity Airport from the associated Id and delete the corresponding element from the database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean deleteAirport(int id) {
		try{
			em.remove(em.find(Airport.class, id));
			em.flush();
		}
		//Catch exceptions in the delete operation from database
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	/**
	 * Create entity Airport from corresponding AirportDTO and update corresponding element in database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean updateAirport(AirportDTO updatedAirport) {
		if(updatedAirport.getName().equals("") || updatedAirport.getName().startsWith(" ") || updatedAirport.getName()==null)
			return false;
		try{
			em.merge(new Airport(updatedAirport, em.find(Location.class, updatedAirport.getLocationId())));
			em.flush();
		}
		//catch exceptions in the update operation of database
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	/**
	 * Create entity Airport from corresponding AirportDTO and insert new corresponding element in database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean insertAirport(AirportDTO newAirport) {
		if(newAirport.getName().equals("") || newAirport.getName().startsWith(" ") || newAirport.getName()==null)
			return false;
		try{
			em.persist(new Airport(newAirport, em.find(Location.class, newAirport.getLocationId())));
		}
		//catch exceptions in the insert operation in database
		catch(Exception e){
			return false;
		}
		return true;
	}

	
}
